<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Test extends MY_Controller{
	function __construct(){
		parent::__construct();
		$this->load->model('poll_model');
		$this->load->library('poll_lib');
		
		if(ENVIRONMENT != 'development' || $this->input->ip_address() != '127.0.0.1')
			show_403('Invalid Access', 403);
	}
	
	function index()
	{
		preg_match('/s\:7\:\"user_id\"\;s\:\d+\:\"(?P<user_id>\d+)\"/', 'a:3:{s:7:"user_id";s:1:"3";s:8:"username";s:7:"gue.', $matches);
		echo "<pre>";
		print_r($matches);
	}
	
	function view($view, $view2 = '', $view3 = ''){
		if($view2 != '')
			$view .= '/'.$view2;
		if($view3 != '/'.'')
			$view .= $view3;
		$this->load->view($view, $this->data);
	}
	
	function addr_conver_1(){
		echo "<pre>";
		
		$query = $this->db->query("select * from address where level=3");
		$tmp = $query->result();
		$old = array();
		foreach($tmp as $i){
			if(! isset($old[$i->belongs_to]))
				$old[$i->belongs_to] = array();
			$old[$i->belongs_to][$i->name] = $i;
		}
		//print_r($old);
		
		$query = $this->db->query("select gov_name as name, gov_code as id from address_add");
		$tmp = $query->result_array();
		$new = array();
		foreach($tmp as $k => $i)
		{
			$i['belongs_to'] = substr($i['id'], 0, 4).'000000';
			
			if(substr($i['belongs_to'], 0, 2) != '11' && mb_substr($i['name'], -1) != '동') 
				continue;
			
			if(!isset($old[$i['belongs_to']][$i['name']]) && !isset($new[$i['id']])){
				$i['level'] = 3;
				$new[$i['id']] = $i;
			}
		}
		$query = $this->db->query("truncate table tmp");
		$this->db->insert_batch('address', $new);
		$query = $this->db->query("select * from address JOIN tmp where address.id = tmp.id");
		print_r($query->result());
	}
	
	function addr_convert_2()
	{
		$query = $this->db->query("select a3.*, concat_ws(' ', a1.name, a2.name, a3.name) as name_full from address as a3 join address as a2 on a2.id = a3.belongs_to join address as a1 on a1.id = a2.belongs_to");
		$tmp = $query->result();
		$this->load->view('tmp', array('arr'=>$tmp));
	}
}
